package com.gc.ykqx.ui.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.gc.ykqx.R;
import com.gc.ykqx.service.entity.GridItem;
import com.gc.ykqx.ui.adapter.BaseGridViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ada on 2017/4/19.
 */

public class TabView extends RelativeLayout implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemClickListener {

    private static final String TAG = "TabView";
    private Context mContext;
    private RadioGroup firstMenu;//一级菜单 温度 降水 风速 水位
    private GridView secondMenu;// 二级菜单 不同时次
    private RadioGroup preMenu;// 降水菜单
    private RadioButton qyPreRb;//区域站降水按钮
    private OnFinishChooseListener listener;//选择完的监听器
    private BaseGridViewAdapter adapter;//适配器
    private List<GridItem> gridItemList;//时次内容
    private int firstPosition;// 0 温度 1 气象降水 2水文降水 3 全部降水 4风速 5水位
    private String[] gridNames;

    /*
    注意：一定调用addView 不然不显示
     */
    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.tab_bar, null, false);
        addView(view);// TODO: 2017/7/10 important 不加不显示
        firstMenu = (RadioGroup) view.findViewById(R.id.first_menu);
        preMenu = (RadioGroup) view.findViewById(R.id.pre_menu);
        secondMenu = (GridView) view.findViewById(R.id.third_menu);
        qyPreRb = (RadioButton) view.findViewById(R.id.rb_qy_pre);
        init();
    }

    /*
       初始化
     */
    private void init() {
        gridItemList = new ArrayList<>();
        adapter = new BaseGridViewAdapter(mContext, gridItemList);
        secondMenu.setAdapter(adapter);
        gridNames = mContext.getResources().getStringArray(R.array.temItem);
        //设置监听
        firstMenu.setOnCheckedChangeListener(TabView.this);
        preMenu.setOnCheckedChangeListener(TabView.this);
        secondMenu.setOnItemClickListener(TabView.this);
        setDefault();
    }

    /*
    设置默认选项
    */
    private void setDefault() {
        if (gridItemList != null) {
            gridItemList.clear();
        }
        for (int i = 0; i < gridNames.length; i++) {
            GridItem gridItem = new GridItem();
            gridItem.setName(gridNames[i]);
            gridItemList.add(gridItem);
        }
        changeItem(0);
    }

    /*
    改变选择条件后对应的事件
     */
    private void changeItem(int position) {
        String id = "n" + firstPosition + "" + position;
        GridItem gi = gridItemList.get(position);
        if (gi.isChecked() && !id.equals("n111") && !id.equals("n29") && !id.equals("n39")) {
            return;//还是当前选项，并未改变
        }
        for (int i = 0; i < gridItemList.size(); i++) {
            GridItem gridItem = gridItemList.get(i);
            if (i == position) {
                gridItem.setChecked(true);
            } else {
                gridItem.setChecked(false);
            }
        }
        adapter.notifyDataSetChanged();
        if (listener != null) {

            listener.onFinishChosen(id);
        }
    }


    /*
    调用者设置的监听
     */
    public void setOnFinishChooseListener(OnFinishChooseListener listener) {
        this.listener = listener;
    }

    /*
    菜单监听事件
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_tem:
                firstPosition = 0;//设置索引位置
                preMenu.setVisibility(View.GONE);//隐藏降水菜单
                secondMenu.setVisibility(VISIBLE);//显示时次按钮
                gridNames = mContext.getResources().getStringArray(R.array.temItem);//获取二级菜单名字
                secondMenu.setNumColumns(5);//二级菜单5个选项
                break;
            case R.id.rb_pre:
                firstPosition = 1;
                preMenu.setVisibility(View.VISIBLE);
                secondMenu.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.preQyItem);
                secondMenu.setNumColumns(6);
                qyPreRb.setChecked(true);//选中区域站降水
                break;
            case R.id.rb_qy_pre:
                firstPosition = 1;
                preMenu.setVisibility(View.VISIBLE);
                secondMenu.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.preQyItem);
                secondMenu.setNumColumns(6);
                break;
            case R.id.rb_sw:
                firstPosition = 2;
                preMenu.setVisibility(View.VISIBLE);
                secondMenu.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.preSwItem);
                secondMenu.setNumColumns(5);
                break;
            case R.id.rb_all:
                firstPosition = 3;
                preMenu.setVisibility(View.VISIBLE);
                secondMenu.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.preSwItem);
                secondMenu.setNumColumns(5);
                break;
            case R.id.rb_wind:
                firstPosition = 4;
                preMenu.setVisibility(View.GONE);
                secondMenu.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.windItem);
                secondMenu.setNumColumns(3);
                break;
            case R.id.rb_res:
                firstPosition = 5;
                preMenu.setVisibility(View.GONE);
                secondMenu.setVisibility(GONE);
                if (listener != null) {
                    listener.onFinishChosen("n50");
                }
                // gridNames = mContext.getResources().getStringArray(R.array.resItem);
                return;
        }
        setDefault();
    }

    /*

     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        changeItem(position);
    }


    /*
    自定义接口,需调用者实现 获取选择内容
     */
    public interface OnFinishChooseListener {
        void onFinishChosen(String selectId);
    }
}
